home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / program / pcl4w13.zip / PAINT.C < prev    next >
Text File  |  1996-07-14  |  4KB  |  173 lines

  1. /*** paint ***/
  2.  
  3. #include "windows.h"
  4. #include <string.h>
  5. #include "paint.h"
  6. #include "ascii.h"
  7.  
  8. extern HWND hMainWnd;
  9.  
  10. #define MIN(a,b) ((a<=b)?(a):(b))
  11.  
  12. /* private variables */
  13.  
  14. static int TheRow = 0;     /* current row */
  15. static int TheCol = 0;     /* current col */
  16. static int TopRow;
  17. static int LeftCol;
  18. static int RightCol;
  19. static char Buffer[NROWS][NCOLS];  /* display buffer */
  20. static char *RowPtr[NROWS];        /* array of row pointers */
  21. static TEXTMETRIC tm;
  22. static int CharHeight;
  23. static int CharWidth;
  24. static char EndOfLine[1] = {LF};
  25.  
  26. /* public */
  27.  
  28. int GetYposition(void)
  29. {return(TheRow*CharHeight);
  30. }
  31.  
  32. int GetXposition(void)
  33. {return(TheCol*CharWidth);
  34. }
  35.  
  36. void InitPaint()
  37. {int Col;
  38.  int Row;
  39.  HDC hDC;
  40.  hDC = GetDC(hMainWnd);
  41.  SelectObject(hDC, GetStockObject(OEM_FIXED_FONT) );
  42.  GetTextMetrics(hDC,&tm);
  43.  CharHeight = tm.tmHeight + tm.tmExternalLeading;
  44.  CharWidth = tm.tmMaxCharWidth;
  45.  ReleaseDC(hMainWnd,hDC);
  46.  /* initialize screen buffer */
  47.  for(Row=0;Row<NROWS;Row++)
  48.    {for(Col=0;Col<NCOLS;Col++) Buffer[Row][Col] = ' ';
  49.     RowPtr[Row] = &Buffer[Row][0];
  50.    }
  51. } /* end InitPaint */
  52.  
  53. void PaintMain(HDC hDC,PAINTSTRUCT *ps)
  54. {int Row;
  55.  int FirstRow;
  56.  int FirstCol;
  57.  int NbrRows;
  58.  int NbrCols;
  59.  int ColWidth;
  60.  int X;
  61.  int Y;
  62.  RECT rect;
  63.  /* compute row & col stuff */
  64.  FirstRow = ps->rcPaint.top  / CharHeight;
  65.  FirstCol = ps->rcPaint.left / CharWidth;
  66.  NbrRows = (ps->rcPaint.bottom - ps->rcPaint.top)  / CharHeight;
  67.  ColWidth = ps->rcPaint.right  - ps->rcPaint.left;
  68.  NbrCols = MIN(NCOLS,(1+ColWidth) / CharWidth);
  69.  X = ps->rcPaint.left;
  70.  /* consider each row */
  71.  for(Row=FirstRow;Row<FirstRow+NbrRows;Row++)
  72.    {/* paint part of row */
  73.     if((Row>=0)&&(Row<NROWS))
  74.       {/* good row number */
  75.        Y = CharHeight*Row;
  76.        /* compute bounding rectangle */
  77.        rect.left = X;
  78.        rect.top  = Y;
  79.        rect.right  = X + ColWidth;
  80.        rect.bottom = Y + CharHeight;
  81.        /* paint it */
  82.        SetBkMode(hDC,OPAQUE);
  83.        ExtTextOut(hDC,X,Y,ETO_OPAQUE|ETO_CLIPPED,&rect,RowPtr[Row]+FirstCol,NbrCols,NULL);
  84.       }
  85.    }
  86. } /* end PaintMain */
  87.  
  88. void DoTheScroll(void)
  89. {int Row;
  90.  int Col;
  91.  char *Ptr;
  92.  RECT rect;
  93.  /* scroll display buffer */
  94.  TheRow = NROWS-1;
  95.  Ptr = RowPtr[0];
  96.  for(Row=0;Row<NROWS-1;Row++) RowPtr[Row] = RowPtr[Row+1];
  97.  RowPtr[NROWS-1] = Ptr;
  98.  for(Col=0;Col<NCOLS;Col++) *Ptr++ = ' ';
  99.  /* scroll the display */
  100.  ScrollWindow(hMainWnd,0,0-CharHeight,NULL,NULL);
  101.  /* invalidate last row */
  102.  rect.left = 0;
  103.  rect.top  = CharHeight * (NROWS-2);
  104.  rect.right  = CharWidth * (RightCol+1);
  105.  rect.bottom = CharHeight * (NROWS-1);
  106.  InvalidateRect(hMainWnd,&rect,TRUE);
  107.  /* reset boundary */
  108.  TopRow = TheRow;
  109.  LeftCol = TheCol;
  110.  RightCol = TheCol;
  111. } /* end DoTheScroll */
  112.  
  113. void WriteTheString(char *String,int Count)
  114. {int i;
  115.  char TheChar;
  116.  RECT rect;
  117.  TopRow = TheRow;
  118.  LeftCol = TheCol;
  119.  RightCol = TheCol;
  120.  for(i=0;i<Count;i++)
  121.    {TheChar = *String++;
  122.     switch(TheChar)
  123.      {case BS:
  124.         if(TheCol>0)
  125.           {*(RowPtr[TheRow]+TheCol) = ' ';
  126.            TheCol--;
  127.           }
  128.         break;
  129.       case LF:
  130.         /* next line */
  131.         if(++TheRow>=NROWS) DoTheScroll();
  132.         /*break;*/
  133.       case CR:
  134.         TheCol = 0;
  135.         LeftCol = 0;
  136.         break;
  137.       default:
  138.         /* put char into display buffer */
  139.         *(RowPtr[TheRow]+TheCol) = (char)TheChar;
  140.         /* increment 'cursor' */
  141.         if(++TheCol>=NCOLS)
  142.           {/* next line */
  143.            TheCol = 0;
  144.            LeftCol = 0;
  145.            if(++TheRow>=NROWS) DoTheScroll();
  146.           }
  147.         else RightCol++;
  148.         break;
  149.      } /* end switch */
  150.    } /* end for */
  151.  /* compute invalid rectangle */
  152.  if((TopRow!=TheRow)||(LeftCol!=TheCol)||(RightCol!=TheCol))
  153.    {rect.left = CharWidth * LeftCol;
  154.     rect.top  = CharHeight * TopRow;
  155.     rect.right  = CharWidth * (RightCol+1);
  156.     rect.bottom = CharHeight * (TheRow+1);
  157.     InvalidateRect(hMainWnd,&rect,TRUE);
  158.    }
  159. }  /* end WriteTheString */
  160.  
  161. void DisplayLine(char *Ptr)
  162. {WriteTheString(Ptr,strlen(Ptr));
  163.  WriteTheString(EndOfLine,1);
  164. } /* end DisplayLine */
  165.  
  166. void DisplayString(char *Ptr)
  167. {WriteTheString(Ptr,strlen(Ptr));
  168. }
  169.  
  170. void DisplayChar(char Chr)
  171. {WriteTheString(&Chr,1);
  172. }
  173.